package com.un.ebs.purchase.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.ReportUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.inventory.domain.InventoryNoteType;
import com.un.ebs.inventory.domain.InventoryNoteTypeExt;
import com.un.ebs.inventory.dto.InventoryNoteVo;
import com.un.ebs.inventory.enums.InventoryTypeEnum;
import com.un.ebs.inventory.service.InventoryNoteService;
import com.un.ebs.inventory.service.InventoryNoteTypeService;
import com.un.ebs.production.domain.ProductionOrderItemExt;
import com.un.ebs.production.domain.ProductionWorkOrderOutExt;
import com.un.ebs.production.service.ProductionOrderService;
import com.un.ebs.purchase.domain.*;
import com.un.ebs.purchase.dto.*;
import com.un.ebs.purchase.service.PurchaseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-05-30
 */
@RestController
@RequestMapping("/purchaseOrder")
@Api(value = "purchase-order", description = "采购订单")
public class PurchaseOrderController {

    @Resource
    private PurchaseOrderService service;

    @Resource
    private ProductionOrderService productionOrderService;

    @Resource
    private InventoryNoteTypeService inventoryNoteTypeService;

    @PostMapping("add")
    @ApiOperation(value = "添加", notes = "添加")
    public WebResult save(@RequestBody @Validated PurchaseOrderAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("addByContract")
    @ApiOperation(value = "添加", notes = "根据采购合同创建")
    public WebResult addByContract(@RequestParam String contractId) {
        return WebUtils.success(service.addByContract(contractId));
    }

    @PostMapping("addByRequirement")
    @ApiOperation(value = "添加", notes = "根据采购需求")
    public WebResult addByRequirement(@RequestBody @Validated PurchaseOrderAddBySimpleDto req) {
        return WebUtils.success(service.add(req));
    }

    @GetMapping("queryInventoryByReady")
    @ApiOperation(value = "添加", notes = "预生成采购入库单")
    public WebResult<PurchaseInventoryInReadyVo> queryInventoryByReady(@RequestParam String purchaseOrderId) {
        return WebUtils.success(service.queryInventoryInReady(purchaseOrderId));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改", notes = "修改")
    public WebResult update(@RequestBody @Validated PurchaseOrderUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除", notes = "根据主键删除单个 PurchaseOrder")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("submit/{id}")
    @ApiOperation(value = "提交")
    public WebResult submit(@PathVariable("id") String id) {
        service.submit(id);
        return WebUtils.success();
    }

    @PostMapping("submitCancel/{id}")
    @ApiOperation(value = "撤销提交")
    public WebResult submitCancel(@PathVariable("id") String id) {
        service.submitCancel(id);
        return WebUtils.success();
    }

    @PostMapping("addServices")
    @ApiOperation(value = "生产服务性采购")
    public WebResult addServices() {
        for (ProductionWorkOrderOutExt item : productionOrderService.queryItemBySeqSku(null)) {
            service.addByService(item.getProductionOrderId(), item.getProductionOrderItemId(), item.getProductionSeqItemId());
        }
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "根据主键获取单个 PurchaseOrder")
    public WebResult<PurchaseOrderVo> getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryViewById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询", notes = "")
    public WebResult<IPage<PurchaseOrderExt>> list(@RequestParam(required = false) String serachText, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.query(StringUtils.isNullOrEmpty(serachText) ? null : serachText.trim(), pageSize, pageIndex));
    }

    @GetMapping("listItem")
    @ApiOperation(value = "查询明细", notes = "")
    public WebResult<IPage<PurchaseOrderItemExt>> listItem(@RequestParam String id, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItemById(id, pageSize, pageIndex));
    }

    @PostMapping("listItemQuery")
    @ApiOperation(value = "查询明细", notes = "")
    public WebResult<IPage<PurchaseOrderItemExt>> listItemQuery(@RequestBody PurchaseOrderItemQueryDto req) {
        return WebUtils.success(service.queryItemView(req));
    }

    @GetMapping("listDeliverPlan")
    @ApiOperation(value = "查询，采购订单交付计划")
    public WebResult<IPage<PurchaseOrderDeliverPlanExt>> listDeliverPlan(@RequestParam(required = false) String searchText, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryDeliverPlan(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), pageSize, pageIndex));
    }

    @GetMapping("listDeliverPlanByOrder")
    @ApiOperation(value = "查询，采购订单交付计划")
    public WebResult<IPage<PurchaseOrderDeliverPlanExt>> listDeliverPlanByOrder(@RequestParam String purchaseOrderId) {
        return WebUtils.success(service.queryDeliverPlanByOrder(purchaseOrderId));
    }

    @GetMapping("listSummary")
    @ApiOperation(value = "查询，供应商采购汇总")
    public WebResult<IPage<PurchaseSummaryExt>> listSummary(@RequestParam String searchText, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.querySummary(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), pageSize, pageIndex));
    }

    @GetMapping("listSummaryItem")
    @ApiOperation(value = "查询，按SKU汇总")
    public WebResult<IPage<PurchaseSummaryItemExt>> listSummaryItem(@RequestParam String id, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.querySummaryItem(id, pageSize, pageIndex));
    }

    @GetMapping("listItemBySupplier")
    @ApiOperation(value = "查询，按订单明细")
    public WebResult<IPage<PurchaseOrderItemExt>> listItemBySupplier(@RequestParam String id, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItemBySupplier(id, pageSize, pageIndex));
    }

    @GetMapping("listInNote")
    @ApiOperation(value = "查询，采购入库单，财务口径")
    public WebResult<IPage<PurchaseInNoteToFinance>> queryPurchaseInNote(@RequestParam String searchText, @RequestParam Integer accountYear, @RequestParam Integer accountMonth, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryPurchaseInNote(searchText, accountYear, accountMonth, pageSize, pageIndex));
    }

    @GetMapping("queryPurchaseInNoteBySupplier")
    @ApiOperation(value = "查询，采购入库单，财务口径")
    public WebResult<IPage<PurchaseInNoteExt>> queryPurchaseInNoteBySupplier(@RequestParam String entId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryPurchaseInNoteBySupplier(entId, pageSize, pageIndex));
    }

    @GetMapping("queryNotContractOrder")
    @ApiOperation(value = "查询，无合同订单")
    public WebResult<IPage<PurchaseOrderExt>> queryNotContractOrder(@RequestParam String entId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryNotContractOrder(entId, pageSize, pageIndex));
    }

    @GetMapping("print")
    @ApiOperation(value = "print", notes = "根据主键获取单个 CustomerTrace")
    public WebResult print(@RequestParam String id, @NotNull HttpServletResponse response) {
        PurchaseOrderVo note = service.queryViewById(id);
        InventoryNoteTypeExt noteType = inventoryNoteTypeService.queryOneByType(InventoryTypeEnum.PURCHASE);
        if (noteType == null) {
            throw new BusinessException("没有找到采购订单的业务类别定义。");
        }
        if (StringUtils.isNullOrEmpty(noteType.getPrintTemplate())) {
            throw new BusinessException("采购订单的打印模板没有指定。");
        }
        ReportUtils.print(note, note.getItems(), noteType.getPrintTemplate(), response);
        return WebUtils.success();
    }
}